


import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gczn_hd/app/core/values/text_styles.dart';
import 'package:gczn_hd/app/core/widget/custom_title_bar.dart';
import 'package:gczn_hd/app/core/widget/gczn_checkbox.dart';
import 'package:gczn_hd/app/core/widget/gczn_radio.dart';
import 'package:gczn_hd/app/modules/eam/spotcheck_detail/controllers/spotcheck_detail_controller.dart';
import 'package:get/get.dart';

import '../../../../core/values/app_colors.dart';
import '../../../../core/values/app_values.dart';

class CheckList extends StatelessWidget{
  final SpotcheckDetailController controller = Get.put(SpotcheckDetailController());
  final Size boxSize = Size(360.w, 60.h);


  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const CustomTitleBar(title: "点检标准明细"),
        Expanded(
          flex: 1,
          child: SingleChildScrollView(
            child: Column(
              children: [
                if (controller.pageType.value == 2)...[
                  _buildSpotPerson(),
                ],
                _buildSpotList()
              ],
            ),
          ),
        )
      ],
    );
  }


  Widget _buildSpotPerson(){

    return Container(
      padding: EdgeInsets.symmetric(vertical: AppValues.size26.h),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _buildSearchSelectItem(title: "点检人员", selected: controller.selectLine.value, list: controller.seleces, onChanged: (_){
            controller.selectLine.value = _!;
          }),
          _buildSearchSelectItem(title: "点检时间", selected: controller.selectLine.value, list: controller.seleces, onChanged: (_){
            controller.selectLine.value = _!;
          }),
        ],
      ),
    );
  }

  Widget _buildSearchSelectItem({required String title, required String selected, required List<String> list, required Function(String? newValue) onChanged}){

    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Text(title, style: textStyle,),
        SizedBox(width: AppValues.margin_10.w,),
        Container(
          width: boxSize.width,
          height: boxSize.height,
          decoration: BoxDecoration(
            border: Border.all(
              color: const Color.fromRGBO(39, 65, 80, 1),
              width: 1.0,
            ),
            borderRadius: BorderRadius.circular(AppValues.margin_12.r),
            color: AppColors.processBtn,//const Color.fromRGBO(12, 29, 46, 1),
          ),
          child: Row(
            children: [
              SizedBox(width: AppValues.size12.w,),
              Expanded(child: DropdownButton(
                value: selected,
                padding: EdgeInsets.zero,
                underline: Container(height: 0,),
                iconSize: 0,
                style:  submitButtonText.copyWith(fontSize: AppValues.size24.sp),
                items: list.map<DropdownMenuItem<String>>((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                }).toList(),
                onChanged: (String? newValue){
                  onChanged(newValue);
                },
                dropdownColor: AppColors.gcznPrimaryBlueBg,
              ),),
              SizedBox(width: 6.w,),
              Icon(Icons.arrow_forward_ios_outlined, size: AppValues.iconSize_18.sp, color: AppColors.colorWhite,),
              SizedBox(width: AppValues.size12.w,)
            ],
          ),
        )
      ],
    );



  }



  Widget _buildSpotList(){
    List<int> list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
    List<Widget> widgets = [];

    for( int i = 0; i < list.length; i++ ){
      String index = (i+1).toString();
      widgets.add(Container(
        width: double.infinity,
        padding: EdgeInsets.only(top: AppValues.size26.h, bottom: AppValues.size26.h),
        decoration: BoxDecoration(
          border: Border(bottom: BorderSide(color: const Color.fromRGBO(39, 65, 80, 1), width: 1.w))
        ),
        child:Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text("$index/${list.length}   开合模装置正常开合" , style: textStyle, maxLines: 1, overflow: TextOverflow.ellipsis,),
            SizedBox(height: AppValues.size14.h,),
            Text("检查方法：听、看、查；  检查标准：正常运行无异响", style: textStyle20Opa.copyWith(fontSize: AppValues.size24.sp),),
            if( controller.pageType.value == 2 )...[
              _buildHandleWidget(i)
            ]
          ],
        ),
      ));
    }

    return Column(
      children: widgets,
    );
  }


  Widget _buildHandleWidget(int idx){


    return Row(
      children: [
        Text("检查结果: ", style: textStyle,),
        if( idx == 1 )
          _buildRadio()
        else if( idx == 2 )
          // Container()
          _buildInput(value: controller.inputValue.value, onChanged: (String value){
            controller.inputValue.value = value;
          })
        else 
          _buildCheckbox()
      ],
    );
  }


  Widget _buildInput({required String value, TextInputType type = TextInputType.text, required ValueChanged<String> onChanged}){
    final Size inputSize = Size(350.w, 60.h);

    return SizedBox(
        width: inputSize.width,
        height: inputSize.height,
            child: TextField(
              controller: TextEditingController(text: value),
              keyboardType: type,
              textAlign: TextAlign.left,
              onChanged: onChanged,
              style: whiteTextStyle,
              cursorColor: AppColors.colorPrimary,
              decoration: InputDecoration(
                  isCollapsed: false,
                  hintText: "请输入",
                  hintStyle: textFieldHintStyle,
                  border: const OutlineInputBorder(
                      borderSide: BorderSide(color: AppColors.counterBtn),
                      borderRadius: BorderRadius.all(Radius.circular(4))
                  ),
                  focusedBorder: const OutlineInputBorder(
                    borderSide: BorderSide(color: AppColors.counterBtn),
                  ),
                  contentPadding:  const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0)
              ),
            ),
    );
  }

Widget _buildCheckbox(){

  return Obx(() => Expanded(
    flex: 1,
    child: Wrap(
      spacing: AppValues.size12,
      runSpacing: AppValues.size12,
      children: controller.seleces.map((item) => GcznCheckbox(value: controller.selectCheckbox.contains(item), label: item, onChanged: (Map<String, dynamic> data){
          if (!controller.selectCheckbox.contains(item)) {
            controller.selectCheckbox.add(data['label']);
          }else {
            controller.selectCheckbox.remove(data['label']);
          }
      })).toList(),
    ),
  ));
}


  Widget _buildRadio(){
    return Obx(() => Expanded(
          flex: 1,
          child: Wrap(
            spacing: AppValues.size12,
            runSpacing: AppValues.size12,
            children: controller.seleces.map((item) => GcznRadio(value: item, label: item, onChanged: (String val){
              controller.selectLine.value = val;
            }, groupValue: controller.selectLine.value,)).toList(),
            ),
          ));
  }

  
}